package com.example.daibanshiwu.controller;

import com.example.daibanshiwu.Service.ArchiveService;
import com.example.daibanshiwu.pojo.Archive;
import com.example.daibanshiwu.pojo.ArchiveDTO;
import com.example.daibanshiwu.pojo.Log;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/archive")
public class ArchiveController {
    private final ArchiveService archiveService;

    public ArchiveController(ArchiveService archiveService) {
        this.archiveService = archiveService;
    }

    //查询所有成员的统计（系统）
    @GetMapping("/getAllArchive")
    public ResponseEntity<List<Archive>> getAllArchive() {
        try {
            List<Archive> archives = archiveService.getAllArchive();
            return ResponseEntity.ok(archives);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果查询出错，可以返回一个空列表和服务器错误状态码
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
    //插入统计数据信息
    @PostMapping("/add")
    public ResponseEntity<?> addArchive(@RequestBody Archive archive) {
        try {
            archiveService.createArchive(archive);
            return ResponseEntity.status(HttpStatus.CREATED).body(archive);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回 500 服务器内部错误，并附带错误信息
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("创建统计失败: " + e.getMessage());
        }
    }

    //查询个人成员的统计
    @GetMapping("/getPersonArchive/{archiverUserId}")
    public ResponseEntity<List<Archive>> getPersonArchive(@PathVariable Long archiverUserId) {
        try {
            System.out.println(">>> [Service] 开始查询用户ID: " + archiverUserId);
            List<Archive> archive = archiveService.getPersonArchive(archiverUserId);
            return ResponseEntity.ok(archive);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果查询出错，可以返回一个空列表和服务器错误状态码
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    //查询联合实体类
    @GetMapping("/selectAll")
    public ResponseEntity<List<ArchiveDTO>> listAllArchives() {
        List<ArchiveDTO> archiveList = archiveService.getArchiveList();
        return ResponseEntity.ok(archiveList);
    }
    //个人统计查询联合
    @GetMapping("/selectPersonal/{userId}")
    public ResponseEntity<List<ArchiveDTO>> listPersonalArchives(@PathVariable Long userId) {
        // 3. 直接使用从URL中获取的 userId，不再需要 request 或 AuthUtil
        if (userId == null || userId <= 0) {
            // (可选但推荐) 添加一个简单的验证
            return ResponseEntity.badRequest().build(); // 返回400错误
        }
        System.out.println(">>> [Service] <URL-PARAM> Personal Archives Query for User ID: " + userId);
        // 调用服务层，传入获取到的 userId
        List<ArchiveDTO> archiveList = archiveService.getArchiveListForUser(userId);
        return ResponseEntity.ok(archiveList);
    }
}
